<?php
require 'Admin/Model/Model.php';
	class VideoModel extends Model{
		function __Construct(){
			parent::__Construct();
            $this->mod = 'videos';
		}

		/*
		 * 根据ID查用户信息
		 * */
		function getByID($id){
		    $sql = "select * from videos where vid = {$id}";
            $result = $this->conn->query($sql);
            return $result;
        }
        /*
         * 添加视频
         * */
        function add($data){
            if(!empty($_FILES["pic"]["name"])){
                $pic = $this->uploadfile('pic','./images/',array('jpg','gif','png'));
            }
            else{
                $pic='';
            }
//            var_dump($data);
            $video = array(
                'videoname' => trim($data['videoname']),
                'vid' => "",
                'tid' => $data['videotype'],
                'uploaddate' => date("YmdHis"),
                'intro' => $data['videointro'],
                'hittimes' => 0,
                'downtimes' => 0,
                'address' => $data['address'],
                'pic'=>$pic,
                'uploadadmin' => '2'
            );
            $keys = implode(',',array_keys($video));
            $values = implode("','", array_values($video));
            $sql="insert into {$this->mod} ({$keys}) VALUES ('{$values}')";
            $result = $this->conn->query($sql);
            return $result;

        }
        /*
         * 更新视频信息
         * */
        function update($data){
            $video = array(
                'videoname' => trim($data['uname']),
                'vid' => $data['vid'],
                'tid' => $data['videotype'],
                'intro' => $data['intro'],
                'uploaddate' =>date("YmdHis"),
                'uploadadmin' =>'2',
                'hittimes' =>'0',
                'downtimes' =>'0',
                'address' =>$data['address']
            );
            if(!empty($_FILES["pic"]["name"])){
                $pic = $this->uploadfile('pic','./images/',array('jpg','gif','png'));
                $video['pic'] = $pic;
            }


            $sets = '';
            foreach ($video as $key=>$val){
                $sets.=$key."='".$val."',";
            }
            $sets=rtrim($sets,','); //去掉SQL里的最后一个逗号

            $sql="UPDATE {$this->mod} SET {$sets} where vid=".$data['vid'];
            //执行SQL语句
            $result = $this->conn->query($sql);
            return $result;

        }
        /*
         * 删除视频
         * */
        function del($id){
            $sql = "delete from {$this->mod} where vid=".$id;
            $result = $this->conn->query($sql);
            return $result;
        }

        function getVideoType(){
            $sql = "select * from videotype";
            $data = $this->conn->query($sql);
            return $data;
        }

        function getTypeByID($id){
            $sql = "select * from videotype where tid = {$id}";
            $result = $this->conn->query($sql);
            return $result;
        }

        /*
         * 更新视频类别信息
         * */
        function updateType($data){
            $videoType = array(
                'tid' => $data['tid'],
                'typename' =>$data['typename']
            );
            $sets = '';
            foreach ($videoType as $key=>$val){
                $sets.=$key."='".$val."',";
            }
            $sets=rtrim($sets,','); //去掉SQL里的最后一个逗号

            $sql="UPDATE  videotype SET {$sets} where tid=".$data['tid'];
            //执行SQL语句
            $result = $this->conn->query($sql);
            return $result;

        }
	}
?>